package org.pmt.first.resource

import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import androidx.navigation.NavHostController
import androidx.navigation.toRoute
import kotlinx.serialization.Serializable
import org.pmt.first.kms.navigation.EnumNavType
import kotlin.enums.enumEntries

/**
* Author: pengmutian
* Date: 2025/2/24 15:45
* Description: RouteRes
*/
@Serializable
sealed class NavRoutes {
    @Serializable
    data object HOME : NavRoutes()
    @Serializable
    data class DETAIL(val route: String) : NavRoutes()
    @Serializable
    data class SETTING(val route: String) : NavRoutes()
//    DETAIL("detail/{itemId}"),
//    SETTING( "setting")
}

@Immutable
enum class SettingsTab {
    APPEARANCE,
    THEME,
    UPDATE,

    PLAYER,
    MEDIA_SOURCE,
    MEDIA_SELECTOR,
    DANMAKU,

    PROXY,
    BT,
    CACHE,
    STORAGE,

    ABOUT,
    DEBUG,
    ;

    companion object {
        @Stable
        val NavType by lazy(LazyThreadSafetyMode.PUBLICATION) {
            EnumNavType(enumEntries<SettingsTab>())
        }
    }
}

@Stable
fun SettingsTab?.orDefault(isSinglePane: Boolean): SettingsTab? {
    return if (isSinglePane) {
        // 单页模式, 自动选择传入的 tab
        this
    } else {
        // 双页模式, 默认选择第一个 tab, 以免右边很空
        this ?: SettingsTab.entries.first()
    }
}

inline fun <reified T > NavHostController.findLast(): T? {
//    val routeFQN = T::class.qualifiedName ?: return null
    return currentBackStack.value
        .asReversed()
        .firstOrNull()
        ?.toRoute()
}


